package com.easyedu.dean.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.easyedu.dean.entity.excel.Student2Excel;
import com.easyedu.dean.entity.excel.TimeTable2Excel;
import com.easyedu.dean.service.OpenCourseService;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lmr
 **/
@RestController
@RequestMapping("/dean/file/download")
public class FileDownloadController {

     private final OpenCourseService openCourseService;
     public FileDownloadController(OpenCourseService openCourseService) {this.openCourseService = openCourseService;}

     /**
      * 下载学生信息模版
      *
      * @param response 请求
      * @throws IOException
      */
     @GetMapping("/template/student")
     public void creatStudent(HttpServletResponse response) {
          try {
               before(response, "学生信息模版");
               // 设置不关闭流
               EasyExcel.write(response.getOutputStream(), Student2Excel.class).autoCloseStream(Boolean.FALSE)
                       .sheet("模板").doWrite(null);
          } catch (Exception e) {
               after(response);
               Map<String, String> map = new HashMap<>();
               map.put("status", "failure");
               map.put("message", "下载文件失败" + e.getMessage());
               try {
                    response.getWriter().println(JSON.toJSONString(map));
               } catch (IOException ex) {
                    ex.printStackTrace();
               }
          }
     }

     @GetMapping("/template/timetable")
     public void downloadTimeTable(@RequestParam Integer openYear, @RequestParam String semester,
             HttpServletResponse response) {

          List<TimeTable2Excel> timeTable2Excel = openCourseService.getTimeTable2Excel(openYear, semester);
          try {
               before(response, "课表信息");
               // 设置不关闭流
               EasyExcel.write(response.getOutputStream(), TimeTable2Excel.class).autoCloseStream(Boolean.FALSE)
                       .sheet("信息").doWrite(timeTable2Excel);
          } catch (Exception e) {
               after(response);
               Map<String, String> map = new HashMap<>();
               map.put("status", "failure");
               map.put("message", "下载文件失败" + e.getMessage());
               try {
                    response.getWriter().println(JSON.toJSONString(map));
               } catch (IOException ex) {
                    ex.printStackTrace();
               }
          }
     }

     private void after(HttpServletResponse response) {
          // 重置response
          response.reset();
          response.setContentType("application/json");
          response.setCharacterEncoding("utf-8");
     }

     private void before(HttpServletResponse response, String fileName) {
          response.setContentType("application/vnd.ms-excel");
          response.setCharacterEncoding("utf-8");
          // 防止中文乱码
          response.setHeader("Content-disposition",
                  "attachment;filename*=utf-8''" + URLEncoder.encode(fileName, StandardCharsets.UTF_8) + ".xlsx");
     }

}
